package com.black.knight.chess.domain;

import com.black.knight.chess.R;
import com.black.knight.chess.utils.ChessUtil;

public class FEN {
	private boolean isWhitePlay;
	
	private Integer[] fields = new Integer[64];

	public FEN(String fen) {
		int fieldId = 0;
		
		for(int i = 0; i<fen.length(); i++) {
			int numEmpty = -1;
			
			try {
				numEmpty = Integer.parseInt(String.valueOf(fen.charAt(i)));
			} catch (NumberFormatException e) {
			}
			
			if(numEmpty != -1) {
				for(int j = fieldId; j<fieldId + numEmpty; j++) {
					fields[j] = R.drawable.empty;
				}
				
				fieldId = fieldId + numEmpty;
			} else if(fen.charAt(i) == '/') {
				continue;
			} else if(fen.charAt(i) == ' ') {
				if(fen.charAt(i + 1) == 'w') {
					isWhitePlay = true;
				} else if(fen.charAt(i + 1) == 'b') {
					isWhitePlay = false;
				}
				
				break;
			} else {
				fields[fieldId] = ChessUtil.getFigure(fen.charAt(i));
				fieldId++;
			}
		}
	}
	
	public boolean isWhitePlay() {
		return isWhitePlay;
	}

	public void setWhitePlay(boolean isWhitePlay) {
		this.isWhitePlay = isWhitePlay;
	}

	public Integer[] getFields() {
		return fields;
	}

	public void setFields(Integer[] fields) {
		this.fields = fields;
	}
}
